package com.rfid.project.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import biz.appframe.PageJsonBean;
import biz.core.KeyValue;

import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;
import com.rfid.project.service.Process;
import com.rfid.project.service.ProcessEngine;
import com.rfid.project.service.ProcessType;

@Controller
public class ProcessController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProcessController.class);

	@Autowired
	private ProcessEngine processEngine;
	
	@Autowired
	private PeopleService peopleService;

	//返回人员管理界面
	@RequestMapping(value = "/processList")
	public String peoplemgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People> peoples = peopleService.findAll();
		model.addAttribute("peoples", peoples);
		
		List<KeyValue> processTypes = ProcessType.getTypeList();
		model.addAttribute("processTypes",processTypes);
		
		return "processList";
	}
	
	@RequestMapping(value = "/queryProcess")
	public String queryProcess(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "processTypeId", defaultValue = "") String processTypeId,
			@RequestParam(value = "submitPersonId", defaultValue = "") String submitPersonId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model){
		People currentUser = (People)request.getSession().getAttribute("currentUser");
		
		List<Process> processes = processEngine.getCurrentTaskList(processTypeId, submitPersonId, currentUser);
		JSONObject object = new JSONObject();
		object.put("total", processes.size());
		JSONArray models = new JSONArray();
		for (Process process : processes) {
			JSONObject mod = new JSONObject();
			mod.put("processTypeId", process.getProcessTypeId());
			mod.put("processTypeName", process.getProcessTypeName());
			mod.put("submitPersonName", process.getSubmitPerson().getName());
			mod.put("submitTime", com.rfid.project.util.DateUtil.dateAllToString(process.getSubmitTime()));
			mod.put("processStatusId", process.getProcessStatusId());
			mod.put("processStatusName", process.getProcessStatusName());
			mod.put("processTitle", process.getProcessTitle());
			mod.put("bizId", process.getBizId());
			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
}
